package com.rimi.servlet;

import com.rimi.bean.UserBean;
import com.rimi.service.UserService;
import com.rimi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private UserService userService;

    public LoginServlet(){
        userService=new UserServiceImpl();
        System.out.println("创建servlet对象");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跳转登录页面
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        //接收登录信息
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        //调用业务层
        UserBean userBean = userService.login(email, password);
        //对返回结果进行判断
        if(userBean == null){
            //跳转登录页面
            response.sendRedirect("/login.jsp");
            return;
        }
        request.getSession().setAttribute("user",userBean);
        //跳转页面、做出响应
        //登录成功
        response.sendRedirect("/v2/index");
    }

}
